﻿namespace Smart.Utils.Shell
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Runtime.InteropServices.ComTypes;
    using System.Windows.Forms;

    public class ShellShortcut : IDisposable
    {
        private bool disposedValue = false;
        private IShellLink ISL;
        private string LinkPath;
        private const int StringSize = 0x400;
        private const int SW_SHOWMAXIMIZED = 3;
        private const int SW_SHOWMINIMIZED = 2;
        private const int SW_SHOWMINNOACTIVE = 7;
        private const int SW_SHOWNORMAL = 1;

        public ShellShortcut(string LinkPath)
        {
            System.Type typeFromCLSID = System.Type.GetTypeFromCLSID(new Guid("00021401-0000-0000-C000-000000000046"));
            this.ISL = (IShellLink) Activator.CreateInstance(typeFromCLSID);
            this.LinkPath = LinkPath;
            if (File.Exists(LinkPath))
            {
                ((IPersistFile) this.ISL).Load(LinkPath, 0);
            }
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposedValue)
            {
                if (disposing)
                {
                }
                if (this.ISL != null)
                {
                    Marshal.ReleaseComObject(this.ISL);
                    this.ISL = null;
                }
            }
            this.disposedValue = true;
        }

        public static string RemoveNullChar(string Input)
        {
            int index = Input.IndexOf(Convert.ToChar(0));
            if (index < 0)
            {
                return Input;
            }
            return Input.Remove(index);
        }

        public void Save()
        {
            ((IPersistFile) this.ISL).Save(this.LinkPath, true);
        }

        public string Arguments
        {
            get
            {
                string pszArgs = new string(' ', 0x400);
                this.ISL.GetArguments(pszArgs, 0x400);
                return RemoveNullChar(pszArgs);
            }
            set
            {
                this.ISL.SetArguments(value);
            }
        }

        public string Description
        {
            get
            {
                string pszName = new string(' ', 0x400);
                this.ISL.GetDescription(pszName, 0x400);
                return RemoveNullChar(pszName);
            }
            set
            {
                this.ISL.SetDescription(value);
            }
        }

        public Keys Hotkey
        {
            get
            {
                short pwHotkey = 0;
                this.ISL.GetHotkey(ref pwHotkey);
                int num2 = ((pwHotkey & 0xff00) * 0x100) | (pwHotkey & 0xff);
                return (Keys) num2;
            }
            set
            {
                if ((value & ~Keys.KeyCode) == Keys.None)
                {
                    throw new ArgumentException("Hotkey must include a modifier key.?");
                }
            }
        }

        public int IconIndex
        {
            get
            {
                int piIcon = 0;
                string pszIconPath = new string(' ', 260);
                this.ISL.GetIconLocation(pszIconPath, pszIconPath.Length, ref piIcon);
                return piIcon;
            }
            set
            {
                this.ISL.SetIconLocation(this.IconPath, value);
            }
        }

        public string IconPath
        {
            get
            {
                int piIcon = 0;
                string pszIconPath = new string(' ', 260);
                this.ISL.GetIconLocation(pszIconPath, pszIconPath.Length, ref piIcon);
                return RemoveNullChar(pszIconPath);
            }
            set
            {
                this.ISL.SetIconLocation(value, this.IconIndex);
            }
        }

        public string Path
        {
            get
            {
                WIN32_FIND_DATA pfd = new WIN32_FIND_DATA();
                string pszFile = new string(' ', 0x400);
                this.ISL.GetPath(pszFile, 0x400, ref pfd, SLGP_FLAGS.SLGP_UNCPRIORITY);
                return RemoveNullChar(pszFile);
            }
            set
            {
                this.ISL.SetPath(value);
            }
        }

        public IShellLink ShellLink
        {
            get
            {
                return this.ISL;
            }
        }

        public ProcessWindowStyle WindowStyle
        {
            get
            {
                int piShowCmd = 0;
                this.ISL.GetShowCmd(ref piShowCmd);
                switch (piShowCmd)
                {
                    case 3:
                        return ProcessWindowStyle.Normal;

                    case 0x1b:
                        return ProcessWindowStyle.Minimized;
                }
                return ProcessWindowStyle.Normal;
            }
            set
            {
                int iShowCmd = 0;
                switch (value)
                {
                    case ProcessWindowStyle.Normal:
                        iShowCmd = 1;
                        break;

                    case ProcessWindowStyle.Minimized:
                        iShowCmd = 7;
                        break;

                    case ProcessWindowStyle.Maximized:
                        iShowCmd = 3;
                        break;

                    default:
                        throw new ArgumentException("Unsupported ProcessWindowStyle value.?");
                }
                this.ISL.SetShowCmd(iShowCmd);
            }
        }

        public string WorkingDirectory
        {
            get
            {
                string pszDir = new string(' ', 0x400);
                this.ISL.GetWorkingDirectory(pszDir, 0x400);
                return RemoveNullChar(pszDir);
            }
            set
            {
                this.ISL.SetWorkingDirectory(value);
            }
        }

        [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")]
        public interface IShellLink
        {
            void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] string pszFile, int cchMaxPath, ref ShellShortcut.WIN32_FIND_DATA pfd, ShellShortcut.SLGP_FLAGS fFlags);
            void GetIDList(ref IntPtr ppidl);
            void SetIDList(IntPtr pidl);
            void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] string pszName, int cchMaxName);
            void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
            void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] string pszDir, int cchMaxPath);
            void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
            void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] string pszArgs, int cchMaxPath);
            void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
            void GetHotkey(ref short pwHotkey);
            void SetHotkey(short wHotkey);
            void GetShowCmd(ref int piShowCmd);
            void SetShowCmd(int iShowCmd);
            void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int cchIconPath, ref int piIcon);
            void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
            void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
            void Resolve(IntPtr hwnd, ShellShortcut.SLR_FLAGS fFlags);
            void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
        }

        [Flags]
        public enum SLGP_FLAGS
        {
            SLGP_RAWPATH = 4,
            SLGP_SHORTPATH = 1,
            SLGP_UNCPRIORITY = 2
        }

        [Flags]
        public enum SLR_FLAGS
        {
            SLR_ANY_MATCH = 2,
            SLR_INVOKE_MSI = 0x80,
            SLR_NO_UI = 1,
            SLR_NOLINKINFO = 0x40,
            SLR_NOSEARCH = 0x10,
            SLR_NOTRACK = 0x20,
            SLR_NOUPDATE = 8,
            SLR_UPDATE = 4
        }

        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
        public struct WIN32_FIND_DATA
        {
            public int dwFileAttributes;
            public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
            public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
            public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
            public int nFileSizeHigh;
            public int nFileSizeLow;
            public int dwReserved0;
            public int dwReserved1;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)]
            public string cFileName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst=14)]
            public string cAlternateFileName;
        }
    }
}

